/*
==================================================================
* 版权所有 © 2025 CaryXiong
* Copyright (C) 2025 CaryXiong(a1816101108@outlook.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
===================================================================
*/




#include <iostream>
using namespace std;


template<class DataType>
struct Node
{
    DataType data;
    Node<DataType> *next;
};

template<class DataType>
class LinkList{
    public:
        LinkList();
        LinkList(DataType a[], int n);
        int Length();
        DataType Get(int i);
        int Locate(DataType x);
        void Insert(int i, DataType x);
        DataType Delete(int i);
        int Empty();
        void PrintList();
    
    private:
        // 单链表的头指针
        Node<DataType> *first;
};


template<class DataType>
LinkList<DataType> :: LinkList(){
    // 生成头节点
    first = new Node<DataType>;
    // 头节点指针域置空
    first -> next = nullptr;
}

/*头插法*/

/*
template<class DataType>
LinkList<DataType> :: LinkList(DataType a[], int n){
    // 初始化空链表
    first = new Node<DataType>;
    first->next = nullptr;

    for(int i=0; i<n; i++){
        Node<DataType> *s;

        s = new Node<DataType>;
        s->data = a[i];

        s->next = first->next;
        first->next = s;
    }
}
*/


/*尾插法*/
template<class DataType>
LinkList<DataType> :: LinkList(DataType a[], int n){
    first = new Node<DataType>;
    first->next = nullptr;

    // 定义尾指针
    Node<DataType> *r = first;

    for(int i=0; i<n; i++){
        Node<DataType> *s;
        s = new Node<DataType>;
        s->data = a[i];

        r->next = s;
        r = s;
    }
}

template<class DataType>
void LinkList<DataType> :: PrintList(){
    Node<DataType> *p = first;

    while(p->next != nullptr){
        p = p->next;
        cout << p->data << '\t';
    }
    cout << '\n';
}



int main(){
    int a[5] = {1,2,3,4,5};
    
    LinkList<int> L(a, 5);
    L.PrintList();
}